package com.rocksea.app.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;


import com.rocksea.app.MyApplication;
import com.rocksea.app.db.history.HistoryTable;

import static android.content.Context.WINDOW_SERVICE;

/**
 * Created by Mrw on 2017/7/13.
 * UI工具类
 */

public class UiUtils {

    public static final String TEST_CODE = "TEST_CODE";
    public static final String TEST_ARRAY ="TEST_ARRAY";
    public static final String BUNDLE_KEY = "data";


    public static void showToast(String message)
    {
        Toast.makeText(MyApplication.getContext(), message, Toast.LENGTH_SHORT).show();
    }
    /**
     * 带参数的activity的跳转
     * @param context
     * @param cla 字节码
     * @param bundle 要带的数据
     */
    public static void JumpActivity(Context context, Class<?> cla, Bundle bundle)
    {
        Intent intent = new Intent(context,cla);
        intent.putExtra("data",bundle);
        context.startActivity(intent);
    }

    /**
     * 不带参数的activity的跳转
     * @param context
     * @param cla 字节码
     */
    public static void JumpActivity(Context context, Class<?> cla)
    {
        Intent intent = new Intent(context,cla);
        context.startActivity(intent);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     * @param dpValue   dp像素值
     *
     */

    public static int dip2px( float dpValue) {
        final float scale = MyApplication.getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     * @param spValue
     * @return
     */
    public static int sp2px(float spValue) {
        final float fontScale = MyApplication.getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
    /**
     * 获取屏幕宽
     *
     * */
    public static int getDisPlayWight(){
        WindowManager manager;
        manager = (WindowManager) MyApplication.getContext().getSystemService(WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }



    public static void showKeyBorad(final Activity activity, final View view)
    {
        activity.getWindow().getDecorView().postDelayed(new Runnable(){
            @Override
            public void run() {
                InputMethodManager inputMethodManager = (InputMethodManager)activity. getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager!=null)
                    inputMethodManager.showSoftInput(view,0);
            }
        },50);
    }

    public static boolean hasNet(Context context)
    {
        ConnectivityManager con = (ConnectivityManager)context.getSystemService("connectivity");
        NetworkInfo workinfo = con.getActiveNetworkInfo();
        return workinfo != null && workinfo.isAvailable();
    }

    public static int judgMentMethod(String applyMethod)
    {
        switch (applyMethod){
            case "1":
                return HistoryTable.TEST_DY;
            case "2":
                return HistoryTable.TEST_GY;
            case "3":
                return HistoryTable.TEST_SC;
            case "4":
                return HistoryTable.TEST_JZ;
            default:
                return -1;
        }
    }

    public static String getVersion()
    {
        try
        {
            PackageManager manager = MyApplication.getContext().getPackageManager();
            PackageInfo info = manager.getPackageInfo(MyApplication.getContext().getPackageName(), 0);
            return info.versionName;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "";
        }
    }


}
